<template>
  <div class="tabs">
    <el-tag
      v-for="(item, index) in tags"
      :key="item.name"
      :closable="item.name !== 'home'"
      :effect="$route.name === item.name ? 'dark' : 'plain'"
      @click="changeMeau(item)"
      @close="handleClose(item, index)"
      size="small"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CommonTag',
  data () {
    return {}
  },
  computed: {
    ...mapState({ tags: state => state.tab.tabsList })
  },
  methods: {
    ...mapMutations({ close: 'closeTag' }),
    //  点击tag跳转功能
    changeMeau (item) {
      this.$router.push({ name: item.name })
    },
    // 点击tag删除功能
    handleClose (item, index) {
      // 通过length与index比较来判断tag位置是不是最后一个
      const length = this.tags.length - 1
      this.close(item) //点击删除的时候调用close
      // 判断与当前路由是否一致，如果与当前路由不一致就不用做额外的操作，直接return
      if (item.name !== this.$router.name) {
        return
      }
      if (index === length) {
        this.$router.push({
          name: this.item[index - 1].name
        })
      } else {
        this.$router.push({
          name: this.item[index].name
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.tabs {
  padding: 10px;
  background-color: #f5f5f5;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
